package com.lujianing.utils;

import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by Administrator on 2014-11-13.
 */
public class StringUtil {


    static Pattern referer_pattern = Pattern.compile("@([^@^\\s^:]{1,})([\\s\\:\\,\\;]{0,1})");//@.+?[\\s:]

    /**
     * 处理提到某人 @xxxx
     *
     * @param msg 传入的文本内容
     * @return 返回带有链接的文本内容
     */
    public static String _GenerateRefererLinks(String msg, List<Map<String, String>> users) {
        StringBuilder html = new StringBuilder();
        int lastIdx = 0;
        Matcher matchr = referer_pattern.matcher(msg);
        while (matchr.find()) {
            String origion_str = matchr.group();

            String str = origion_str.substring(1, origion_str.length()).trim();

            html.append(msg.substring(lastIdx, matchr.start()));


            Map<String, String> user = null;

            for (Map<String, String> u : users) {
                if (u.get("name").equals(str)) {
                    user = u;
                }
            }
            if (user != null) {
                html.append("<a href='" + user.get("url") + "' class='referer' target='_blank'>@");
                html.append(str.trim());
                html.append("</a>");
            } else {
                html.append(origion_str);
            }
            lastIdx = matchr.end();
        }
        html.append(msg.substring(lastIdx));
        return html.toString();
    }


}
